package cn.bgs.serviceedu.schedule;


import cn.bgs.serviceedu.entity.Category;
import cn.bgs.serviceedu.entity.Doctor;
import cn.bgs.serviceedu.entity.Video;
import cn.bgs.serviceedu.service.CategoryService;
import cn.bgs.serviceedu.service.DoctorService;
import cn.bgs.serviceedu.service.VideoService;
import cn.bgs.serviceedu.vo.RedisList;
import cn.bgs.serviceedu.vo.RedisTop;
import cn.bgs.serviceedu.vo.VodList;
import cn.bgs.serviceutils.utils.CSException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @Author:YiChen
 * @program: IntelliJ IDEA
 * @Description:
 * @Created 2021/9/16-15:57
 */
@Component
public class Schedule {

    @Autowired
    private DoctorService doctorService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private VideoService videoService;
//
//    //在每天的10点持久化一次，
    @Scheduled(cron = "0 0 10 * * ? ")
    public void ScheduleTest(){
        //医生热搜榜持久化
        List<RedisList> redisList = doctorService.getRedisList();
        Doctor doctor = new Doctor();
        try {
            for (int i = 0; i < redisList.size(); i++) {
                RedisList redisList1 = redisList.get(i);
                Doctor byId = doctorService.getById(redisList1.getDoctorId());
                if (!byId.getBrowse().equals(redisList1.getValue())) {
                    doctor.setDoctorId(Integer.valueOf(redisList1.getDoctorId()));
                    doctor.setBrowse(redisList1.getValue());
                    doctorService.updateById(doctor);
                }
            }
        } catch (NumberFormatException e) {
            throw new CSException(20001,"修改失败");
        }

        //种类热搜榜持久化
        List<RedisTop> redisTops = categoryService.getRedisList();
        try {
            Category category = new Category();
            for (int i = 0; i < redisTops.size(); i++) {
                RedisTop redisTop = redisTops.get(i);
                Category byId = categoryService.getById(redisTop.getId());
                if (!byId.getBrowse().equals(redisTop.getValue())) {
                    category.setId(Long.valueOf(redisTop.getId()));
                    category.setBrowse(redisTop.getValue());
                    categoryService.updateById(category);
                }
            }
        } catch (NumberFormatException e) {
            throw new CSException(20001,"修改失败");
        }


        List<VodList> vodLists = videoService.getRedisList();
        Video video = new Video();
        try {
            for (int i = 0; i < vodLists.size(); i++) {
                VodList redisList1 = vodLists.get(i);
                Video byId = videoService.getById(redisList1.getVodId());
                if (!byId.getPlayCount().equals(redisList1.getPlayCount())) {
                    video.setVodId(Integer.valueOf(redisList1.getVodId()));
                    video.setPlayCount(redisList1.getPlayCount());
                    videoService.updateById(video);
                }
            }
        } catch (NumberFormatException e) {
            throw new CSException(20001,"修改失败");
        }

    }


}
